import xlrd2


class XlrdHandler:

    def __init__(self, filename):
        self.work = xlrd2.open_workbook(filename)

    # 读取指定xlsx文件sheet页
    def read_sheet(self, sheet_name):
        result_list = []
        for i in range(1, self.work[sheet_name].nrows):  # 遍历指定sheet页总行数
            result = self.work[sheet_name].row_values(i)
            result_list.append(tuple(result))  # 将每一行的列表数据转化为元组数据供数据驱动使用
        return result_list

    # 读取整个xlsx文件
    def read_book(self):
        result_list = []
        for i in self.work.sheets():  # 获取所有sheet并遍历,i为sheet对象
            for r in range(1, i.nrows):  # 获取某sheet总行数并遍历
                result = i.row_values(r)
                result_list.append(tuple(result))
        return result_list


if __name__ == '__main__':
    work = XlrdHandler('../data/cases.xlsx')
    sheet = work.read_sheet('Sheet3')
    print('Sheet3', sheet)
    sheets = work.read_book()
    print(sheets)
